package com.beemans.admob.demo.ui.fragments

import android.util.Log
import com.beemans.common.ui.fragments.CommonFragment
import com.beemans.common.utils.ToastUtils
import com.beemans.admob.ads.AbAdvertManager
import com.beemans.admob.ads.AbInsertAdvert
import com.beemans.admob.ads.AbInsertAdvertCallback
import com.beemans.admob.ads.AbInsertAdvertConfig
import com.beemans.admob.demo.R
import com.beemans.admob.demo.constant.Constant
import com.beemans.admob.demo.databinding.FragmentInsertAdBinding
import com.google.android.gms.ads.ResponseInfo
import com.tiamosu.databinding.delegate.lazyDataBindingOrNull
import com.tiamosu.databinding.page.DataBindingConfig
import com.tiamosu.fly.ext.clickNoRepeat

/**
 * @author tiamosu
 * @date 2020/7/8.
 */
class InsertAdFragment : CommonFragment() {
    private val dataBinding: FragmentInsertAdBinding? by lazyDataBindingOrNull { }

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.fragment_insert_ad)
    }

    override fun initEvent() {
        // 加载广告
        dataBinding?.insertAdBtnPreload?.clickNoRepeat {
            preloadAdvert()
        }

        // 展示广告
        dataBinding?.insertAdBtnShow?.clickNoRepeat {
            showAdvert()
        }
    }

    /**
     * 加载广告
     */
    private fun preloadAdvert() {
        val advertConfig = AbInsertAdvertConfig(Constant.INSERT_ID)

        AbAdvertManager.loadInsertAdvert(
            this,
            advertConfig,
            object : AbInsertAdvertCallback() {
                override fun onAdvertLoaded(abInsertAdvert: AbInsertAdvert) {
                    printLog("广告加载成功")
                }

                override fun onAdvertLoadFail(abInsertAdvert: AbInsertAdvert, adError: String?) {
                    printLog("广告加载失败 --- adError:$adError")
                }
            }
        )
    }

    /**
     * 展示广告
     */
    private fun showAdvert() {
        val advertConfig = AbInsertAdvertConfig(Constant.INSERT_ID)
        advertConfig.isShowAfterLoaded = true
        advertConfig.isPreloadAdvert = true

        AbAdvertManager.loadInsertAdvert(
            this,
            advertConfig,
            object : AbInsertAdvertCallback() {
                override fun onAdvertLoaded(abInsertAdvert: AbInsertAdvert) {
                    printLog("广告加载成功")
                }

                override fun onAdvertLoadFail(abInsertAdvert: AbInsertAdvert, adError: String?) {
                    printLog("广告加载失败 --- adError:$adError")
                }

                override fun onAdvertShow(
                    abInsertAdvert: AbInsertAdvert,
                    responseInfo: ResponseInfo?
                ) {
                    printLog("广告展示 --- responseInfo:$responseInfo")
                }

                override fun onAdvertClicked(
                    abInsertAdvert: AbInsertAdvert,
                    responseInfo: ResponseInfo?
                ) {
                    printLog("广告点击 --- responseInfo:$responseInfo")
                }

                override fun onAdvertClose(
                    abInsertAdvert: AbInsertAdvert,
                    responseInfo: ResponseInfo?
                ) {
                    printLog("广告关闭 --- responseInfo:$responseInfo")
                }
            }
        )
    }

    private fun printLog(msg: String) {
        Log.e("susu", msg)
        ToastUtils.showShort(msg)
    }

    override fun doBusiness() {
    }
}